package com.zigong.app.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.zigong.app.R;
import com.zigong.app.beans.FHistory;
import com.zigong.app.ui.adapter.holder.HistoriesHolder;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2017/6/23 0023.
 */

public class HistoriesAdapter extends BaseHolderAbsAdapter<FHistory, HistoriesHolder> {
    public HistoriesAdapter(Context context, List<FHistory> list) {
        super(context, list);
    }

    @Override
    public HistoriesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_histories, parent, false);
        HistoriesHolder historiesHolder = new HistoriesHolder(context, view);
        historiesHolder.setInternalClick(this);
        return historiesHolder;
    }

    @Override
    public HistoriesAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    @Override
    public void onBindViewHolder(HistoriesHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        FHistory focusHistory = (FHistory) data.get(position);
        Date hDate=new Date(focusHistory.getHistoryTime());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String hTime = format.format(hDate);

        if (position == 0) {
            holder.tvTimeTitle.setText(hTime);
            holder.llTimeTitle.setVisibility(View.VISIBLE);
        } else {
            FHistory previous = (FHistory) data.get(position - 1);
            Date pDate=new Date(previous.getHistoryTime());
            String pTime = format.format(pDate);
            if (pTime.equals(hTime)) {
                holder.llTimeTitle.setVisibility(View.GONE);
            } else {
                holder.tvTimeTitle.setText(hTime);
                holder.llTimeTitle.setVisibility(View.VISIBLE);
            }
        }
        holder.setData(data.get(position));
    }
}
